package com.kapo.egroup.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kapo.egroup.entity.base.user.User;
import com.kapo.egroup.exception.ServiceException;
import com.kapo.egroup.mapper.UserMapper;
import com.kapo.egroup.service.UserService;
import com.kapo.egroup.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Title UserServiceImpl
 * @Description TODO
 * @Author 86159
 * @Date 2022-10-11 20:50
 **/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public User selectByEmail(String email) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email",email);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public User selectByPhone(String phone) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        return baseMapper.selectOne(queryWrapper);
    }

    @Override
    public User selectByUsername(String username) {
        User user = selectByPhone(username);
        if(StringUtils.isNull(user)){
            user = selectByEmail(username);
        }
        if(StringUtils.isNull(user)){
            throw new ServiceException("登录用户：" + username + " 不存在");
        }
        return user;
    }

    @Override
    public User getWechatNumber(Long userId) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("wechat_number").eq("id",userId);
        return baseMapper.selectOne(queryWrapper);
    }
}
